<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2019 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 达达 <ccbox_net@163.com>
// +----------------------------------------------------------------------
namespace app\goods\controller;

use app\goods\service\BrandService;

use app\goods\lib\controller\AdminBaseController;

class AdminBrandController extends AdminBaseController
{
    protected function assignToTpl()
    {
        $this->assign('_title', '品牌');
        $this->assign('nowController', 'AdminBrand');
    }

    /**
     * 品牌列表
     * @adminMenu(
     *     'name'   => '品牌管理',
     *     'parent' => 'goods/AdminIndex/default',
     *     'display'=> true,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '品牌管理入口,品牌列表',
     *     'param'  => ''
     * )
     */
    public function index()
    {
        $service = new BrandService();

        $param = $this->request->param([
            'limit' => 5,
            'status' => 'yes',
            'list_order_by' => 'list_order asc',
            'keyword' => '',
            'id_start' => null,
            'id_end' => null
        ]);
        $list = $service->getSearchList($param, ['*'], $param['list_order_by']);

        $list->appends($param);
        $this->assign('_data', $list);

        $this->assign('httpParam', $param);
        $this->assign('listData', $list->items());
        $this->assign('listTotal', $list->total());
        // $this->assign('pageLimit', $limit);
        $this->assign('pageLimit', $list->listRows());
        $this->assign('pageURL', $list->getCurrentPath());
        $this->assign('pageNow', $list->getCurrentPage());
        $this->assign('pageHtml', $list->render());

        return $this->fetch();
    }

    /**
     * 添加品牌
     * @adminMenu(
     *     'name'   => '添加品牌',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '添加品牌',
     *     'param'  => ''
     * )
     */
    public function add()
    {
        $this->assign('formAction', 'admin/goods/brand');
        $this->assign('formMethod', 'post');
        $this->assign('formData', []);
        return $this->fetch('form');
    }

    /**
     * 编辑品牌
     * @adminMenu(
     *     'name'   => '编辑品牌',
     *     'parent' => 'index',
     *     'display'=> false,
     *     'hasView'=> true,
     *     'order'  => 10000,
     *     'icon'   => '',
     *     'remark' => '编辑品牌',
     *     'param'  => ''
     * )
     */
    public function edit()
    {

        $id = $this->request->param('id', 0, 'intval');
        if ($id > 0) {
            $service = new BrandService();
            $data = $service->read($id);
            if (empty($data)) {
                $this->error('not found!');
            } else {
                $this->assign('formAction', 'admin/goods/brand/' . $id);
                $this->assign('formMethod', 'put');
                $this->assign('formData', $data);
            }
        }
        return $this->fetch('form');
    }

}